<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<jsp:include page="/includes/top.jsp"></jsp:include>
<div class="page-header">
	<h1></h1>
</div>
<!-- /.page-header -->
<div class="row">
	<div class="col-xs-12">
		<form class="form-horizontal" id="comForm">
		  <div style="display: none">
				<input name="company.companyId" type="hidden" />
			</div>
  			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">公司名称</label>
				<div class="col-sm-4">
				   <div class="clearfix input-icon input-icon-right">
					<input type="text" name="company.companyName" class="form-control" placeholder="公司名称" />
				   </div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">固定电话</label>
				<div class="col-sm-4">
				<div class="clearfix input-icon input-icon-right">
					<input type="text" name="company.telephone"  class="form-control"  placeholder="固定电话" />
				</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">移动电话</label>
				<div class="col-sm-4">
				<div class="clearfix input-icon input-icon-right">
					<input type="text" name="company.phone"  class="form-control"  placeholder="移动电话" />
				</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">国家</label>
				<div class="col-sm-4">
				<div class="clearfix input-icon input-icon-right">
					<input type="text" name="company.country"  class="form-control"   placeholder="国家" />
				</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">城市</label>
				<div class="col-sm-4">
				<div class="clearfix input-icon input-icon-right">
					<input type="text" name="company.city"  class="form-control"   placeholder="城市" />
				</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">地址</label>
				<div class="col-sm-4">
				<div class="clearfix input-icon input-icon-right">
					<input type="text" name="company.address"  class="form-control"   placeholder="地址" />
				</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">邮政编码</label>
				<div class="col-sm-4">
				<div class="clearfix input-icon input-icon-right">
					<input type="text" name="company.zipCode"  class="form-control" placeholder="邮政编码" />
				</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">电子邮件</label>
				<div class="col-sm-4">
				   <div class="clearfix input-icon input-icon-right">
					 <input type="text"  name="company.email"  class="form-control"  placeholder="电子邮件" />
				   </div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">公司主页</label>
				<div class="col-sm-4">
				<div class="clearfix input-icon input-icon-right">
					<input type="text" name="company.url"  class="form-control"  placeholder="http://" />
				</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">公司法人</label>
				<div class="col-sm-4">
				<div class="clearfix input-icon input-icon-right">
					<input type="text" name="company.companyPerson"  class="form-control"  placeholder="公司法人" />
				</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">类型</label>
				<div class="col-sm-4">
					<select class="form-control" name="company.companyTypeId">
				    <option value="1">客户</option>
				    <option value="2">厂商</option>
				    <option value="3">供应商</option>
				    <option value="4">政府</option>
				    <option value="5">公司内部</option>
				    <option value="6">其他</option>
				  </select>
				   
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">描述</label>
				<div class="col-sm-4">
				  <div class="clearfix input-icon input-icon-right">
					<input type="text" name="company.describes"  class="form-control" placeholder="描述" />
				</div>
				</div>
			</div>
			<div class="clearfix form-actions">
				<div class="col-md-offset-3 col-md-9">
					<button class="btn btn-info" id="save" type="button" >
						<i class="icon-ok"></i> 保存
					</button>

					&nbsp; &nbsp; &nbsp;
					<button class="btn" type="reset" id="back">
						<i class="icon-undo"></i> 取消
					</button>
				</div>
			</div>
		</form>
	</div>
	<!-- /.col -->
</div>
<jsp:include page="/includes/bottom.jsp"></jsp:include>

<script src="assets/js/bootstrapValidator.min.js"></script>
<script type="text/javascript">
    var cid = getQueryString("id");
	function ininfrom() {
		var dataMap = {
			'company.companyId' : cid
		};
		$.ajax({
			type : "post",
			url : "getcompany",
			data : dataMap,
			dataType : "json",
			success : function(data) {
				if (typeof (data) == "object") {
					$("#comForm input").each(function() {
						var inputname = $(this).attr("name");
						if (inputname != undefined && inputname != "") {
							var colname = inputname.split('.')[1];
							$(this).val(data[colname]);
							if ($(this).attr("type") == "checkbox") {
								if (data[colname] == "1") {
									$(this).prop("checked", true);
								} else {
									$(this).prop("checked", false);
								}
							}
						}
					});
					$("#comForm select").each(function() {
						var inputname = $(this).attr("name");
						if (inputname != undefined && inputname != "") {
							var colname = inputname.split('.')[1];
							$(this).val(data[colname]);
						}
					});
				}
			}
		});
	}
	//添加
	function savafrom(url) {
		$("#save").click(function() {
			$("#comForm").bootstrapValidator("validate");
			if ($("#comForm").data("bootstrapValidator").isValid()) {
				var dataMap = $("#comForm").serializeArray();
				$.ajax({
					type : "post",
					url : url,
					data : dataMap,
					dataType : "json",
					success : function(data) {
						window.open("Company/companylist.jsp?m=" + mid, "_self");
					}
				});
			}
		});
	}
	
	
	$(document).ready(function() {
		if (cid != null && cid != "") {
			ininfrom();
			savafrom("editcompany");
		} else {
			savafrom("addcompany");
		}
		
		$("#back").click(function() {
			window.history.back();
		});
		
		$("#comForm").bootstrapValidator({
			feedbackIcons : {
				valid : 'icon-ok-sign',
				invalid : 'icon-remove-sign',
				validating : 'icon-refresh'
			},
			fields : {
				'company.companyName' : {
					validators : {
						notEmpty : {
							message : '公司名称，不能为空！'
						},
						stringLength : {
							min : 5,
							max : 30,
							message : '公司名称长度必须是5-30字符！'
						}
					}
				},
				'company.phone': {
				    validators: {
				      notEmpty: {
				        message: '电话号码，不能为空！'
				      },
				      regexp : {
						regexp : /^[1][358]\d{9}$/,
						message : '电话号码格式错误,只能输入数字'
						}
				    }
				},
				'company.telephone': {
				    validators: {
				      notEmpty: {
				        message: '固定电话，不能为空！'
				      }
				    }
				},
				'company.address': {
				    validators: {
				      notEmpty: {
				        message: '公司地址，不能为空！'
				      }
				    }
				},
				'company.city': {
				    validators: {
				      notEmpty: {
				        message: '公司所在城市，不能为空！'
				      }
				    }
				},
				'company.country': {
				    validators: {
				      notEmpty: {
				        message: '公司所在国家，不能为空！'
				      }
				    }
				},
				'company.zipCode':{
				    regexp : {
						regexp : /^[1-9]\d{5}(?!\d)$/,
						message : '邮政编码格式错误,只能输入数字'
					}
				}
			}
		});
	});
	
	
</script>
